package fr.afcepf.atod19.dreamproject.business.orchestration;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import fr.afcepf.atod19.dreamproject.business.model.EntityIntervention;
import fr.afcepf.atod19.dreamproject.business.model.Intervention;
import fr.afcepf.atod19.dreamproject.business.model.ListeEntityInterventions;

import java.util.List;

public class OrchestrationImpl implements IOrchestration {
	private Client client;
	
	public OrchestrationImpl() {
		ClientConfig config = new DefaultClientConfig();
		this.client = Client.create(config);
	}

    @Override
	public Intervention TestAppelWS(int idReve) {
		WebResource applicationResource = client.resource("http://localhost:8080/");
		WebResource resourceAddIntervention = applicationResource.path("WebServices/rest/webservice/addIntervention/"+idReve);
		Intervention reply = resourceAddIntervention.accept(MediaType.APPLICATION_JSON).get(Intervention.class);
		return reply;
	}

	@Override
	public String addInterventionBrigade(int idReve, String adresseClient, String nomClient, String prenomClient) {
        WebResource applicationResource = client.resource("http://192.168.11.55:8080/");
        WebResource resourceAddIntervention = applicationResource.path("Brigade/rest/intervention/addIntervention/"+idReve+"/"+adresseClient+"/"+prenomClient+"/"+nomClient);
        String response = resourceAddIntervention.accept(MediaType.TEXT_PLAIN).get(String.class);
        return response;
	}

	@Override
	public Intervention getInterventionByIdReve(int idReve) {
		WebResource applicationResource = client.resource("http://localhost:8080/");
		WebResource resourceStatIntervention =applicationResource.path("Brigade/rest/intervention/countIntervention/"+idReve);
		Intervention retour = resourceStatIntervention.accept(MediaType.APPLICATION_JSON).get(Intervention.class);
		return retour;
	}

	@Override
	public String sendExpedtion(int idCommande, String dateCommande, String adrFacturation, String adrLivraison) {
		WebResource applicationResource = client.resource("http://192.168.11.47:8080/");
		WebResource resourceAddIntervention = applicationResource.path("Expedition_web/rest/commande/addCommande/"+idCommande+"/"+dateCommande+"/"+adrFacturation+"/"+adrLivraison);
		return resourceAddIntervention.accept(MediaType.TEXT_PLAIN).get(String.class);
	}

    @Override
    public List<EntityIntervention> getAllInterventions() {
        WebResource applicationResource = client.resource("http://192.168.11.55:8080/");
        WebResource resourceStatIntervention =applicationResource.path("Brigade/rest/intervention/getInterventions");
        ListeEntityInterventions retour = resourceStatIntervention.accept(MediaType.APPLICATION_JSON).get(ListeEntityInterventions.class);
        return retour.getInterventions();
    }
}
